import getHeight from "./getHeight";
var defaults = {
    insertType: 1,//1=>直接存html字符串
    dataStr: '',
    scrollY: 0,
    appendTo: '', //list的id
    name: 'detail2list',
    extraData: '',
    dataStrName: 'dataStr',
    scrollYName: 'scrollY',
    extraDataName: 'extraData',
    currentTimeStampName: 'currentTimeStamp',
    currentTimeStamp: (new Date()).getTime(),
    addBack: true,//为列表添加一个标志
    expire: 1 * 60 * 1000//过期时间,单位毫秒，默认10分钟
};

class Detail2list{
    constructor(options){//constructor是一个构造方法，用来接收参数
        //this代表的是实例对象
        this.settings = Object.assign(defaults, options || {});
    }
};
Detail2list.prototype = {
    init() {
        if (!('localStorage' in window) && !(window['localStorage'] !== null)) {
            console.error("您的浏览器不支持h5的window.localStorage本地存储，请使用其他现代浏览器");
            return;
        }
        this.isExpire();
        // this.windowBind();
        this.setLocalStorage(0);
    },
    isBack: function () {
        //根据锚点，粗略判断是否点了返回，从detail返回了列表
        return window.location.hash == '' ? false : true;
    },
    isExpire: function () {
        var current = (new Date).getTime();
        if (this.existLocalStorageName()) {
            var storage = this.getLocalStorage();
            var localCurrent = storage[this.settings.currentTimeStampName] + this.settings.expire;
            if (current > localCurrent) {
                this.removeLocalStorage();
                console.log("expire");
            }
        }
    },
    insertHtmlStr: function (str, extraData) {
        this.setLocalStorage(1, str, extraData);
        this.settings.extraData = extraData;
    },
    setLocalStorage: function (type, str, extraData) {
        //type=0=>refresh or back，type=1=>scrolling
        var storage;
        if (this.existLocalStorageName()) {
            //exist
            storage = this.getLocalStorage();
            if (type == 0) {
                console.log(this.settings)
                // 从详情页返回
                if (this.isBack() && this.settings.addBack) {

                    // 插入html字符串
                    if (this.settings.appendTo){
                        document.getElementById(this.settings.appendTo).html(storage[this.settings.dataStrName]);
                    }

                    // 插入vue的data字符串, this.vm是实例化时传入的vue的对象
                    if (storage[this.settings.extraDataName]){
                        //
                        let vmData = this.json2string(storage[this.settings.extraDataName]);
                        console.log(this.settings.extraData)
                        for (let key in vmData) {
                            this.settings.vm.$set(this.settings.vm.$data, key, vmData[key])
                        }
                        this.settings.vm.$nextTick(() => {
                            // if (this.scrollHeight) {
                            //     window.scrollTo(0, this.scrollHeight)
                            // }
                            this.scroll2Y(storage[this.settings.scrollYName]);
                        })
                        return false;
                    } 

                    // localstorage里面有内容，但是pagedata数据为空，重新刷新页面
                    // this.settings.vm.createdInit();
                    // return false;
                    
                }
                // 从其他页面进入，刷新页面
                // this.settings.vm.createdInit();
                // storage[this.settings.extraDataName] = this.isBack() && this.settings.addBack ? storage[this.settings.extraDataName] : '';
                
            }
            else if (type == 1 && extraData !== undefined) {
                this.settings.extraData = extraData;
                storage[this.settings.extraDataName] = extraData;
            }
            storage[this.settings.scrollYName] = this.isBack() && this.settings.addBack ? this.settings.scrollY : 0;
            storage[this.settings.dataStrName] = str === undefined ? storage[this.settings.dataStrName] : str;;
            window.localStorage.setItem(this.settings.name, this.string2json(storage));
        }
        else {
            //not exist
            // if (type == 0) {
            //     this.settings.vm.createdInit();
            // }
            storage = {};
            storage[this.settings.currentTimeStampName] = this.settings.currentTimeStamp;
            storage[this.settings.extraDataName] = this.settings.extraData;
            storage[this.settings.scrollYName] = this.settings.scrollY;
            storage[this.settings.dataStrName] = this.settings.dataStr;
            window.localStorage.setItem(this.settings.name, this.string2json(storage));
        }
    },
    getLocalStorage: function () {
        return this.json2string(window.localStorage.getItem(this.settings.name));
    },
    removeLocalStorage: function () {
        window.localStorage.removeItem(this.settings.name);
    },
    string2json: function (json) {
        return JSON.stringify(json);
    },
    json2string: function (str) {
        return JSON.parse(str);
    },
    existLocalStorageName: function () {
        return window.localStorage.getItem(this.settings.name) === null ? false : true;
    },
    wscoll: function () {
        let scroll_top = getHeight.getScrollTop();
        this.settings.scrollY = scroll_top;
        this.replaceAnchor(scroll_top);
        this.setLocalStorage(1);
    },
    windowBind: function () {
        this.settings.first = true;
        window.addEventListener('scroll', this.wscoll(this));
    },
    replaceAnchor: function (scroll_top) {
        if (this.settings.addBack) {
            //这样锚点滚动就会产生历史记录数
            //window.location.hash="#"+this.settings.scrollYName+"="+scroll_top;
            //这样锚点滚动不会产生历史记录数
            window.location.replace("#" + this.settings.scrollYName + "=" + scroll_top);
        }
    },
    scroll2Y: function (scrolly) {
        window.scrollTo(0, scrolly)
    },

}

export default Detail2list